package com.ymx.darling.session.setting.relation;

import com.ymx.darling.util.LambdaUtil;
import com.ymx.darling.wrapper.fragment.lambda.LambdaGetter;

import java.util.LinkedList;
import java.util.List;

/**
 * 关联查询选择器
 * @author 爱Java的小于
 */
public class RelationSelector {
    private final List<RelationNode> relationNodeList = new LinkedList<>();

    public <T> RelationSelector addRelationField(LambdaGetter<T> field) {
        return addRelationField(field, 1, relationSelector -> relationSelector);
    }

    public <T> RelationSelector addRelationField(LambdaGetter<T> field, int depth) {
        return addRelationField(field, depth, relationSelector -> relationSelector);
    }

    public <T> RelationSelector addRelationField(LambdaGetter<T> field, RelationSelectorFunction relationSelector) {
        return addRelationField(field, 1, relationSelector);
    }

    public <T> RelationSelector addRelationField(LambdaGetter<T> field, int depth, RelationSelectorFunction relationSelector) {
        String fieldName = LambdaUtil.getFieldName(field);
        List<RelationNode> childList = relationSelector.selector(new RelationSelector()).getRelationNodeList();
        this.relationNodeList.add(new RelationNode(fieldName, depth, childList));
        return this;
    }

    public List<RelationNode> getRelationNodeList() {
        return relationNodeList;
    }
}
